// Game Application
// (c) jimon game studio

#include "CGameApplication.h"

#include "IProfiler.h"
#include "IVariables.h"
#include "IScriptManager.h"
#include "IFpsCounter.h"
#include "ISystem.h"
#include "jeScriptResource.h"

namespace je
{
	namespace app
	{
		//! Constructor
		CGameApplication::CGameApplication()
			:Run(false),Perspective_Game(NULL),Perspective_Console(NULL)
		{
			if(!core::GetVariables()->GetVariable("ApplicationDBFileName"))
				je::core::GetVariables()->SetVariable("ApplicationDBFileName","../Media/Game.db3");

			JELOG(jeStringc("DBName : ")+core::GetVariables()->GetVariableString("ApplicationDBFileName").c_str())

			core::IDataBase * DataBase = core::CreateDataBase(core::GetVariables()->GetVariableString("ApplicationDBFileName").c_str());
			media::GetResourceManager()->SetDataBase(DataBase);
			DataBase->Drop();

			if(!core::GetVariables()->GetVariable("Enable_Scripting"))
				core::GetVariables()->SetVariable("Enable_Scripting",1);

			if(core::GetVariables()->GetVariablec8("Enable_Scripting"))
			{
				if(!core::GetVariables()->GetVariable("Script_Compile_Source"))
					core::GetVariables()->SetVariable("Script_Compile_Source",1);

				if(core::GetVariables()->GetVariablec8("Script_Compile_Source"))
				{
					core::IBuffer * Buffer = core::GetFileSystem()->ReadFile("../Media/Game.jas");
					if(Buffer)
					{
						core::GetScriptManager()->LoadScript(Buffer);
						Buffer->Drop();
					}
				}
				else
				{
					media::jeScriptResource * ScriptResource = JE_SCRIPTRESOURCE("Main");
					ScriptResource->AddUsage();
					core::GetScriptManager()->LoadByteCode(ScriptResource->GetScript());
					ScriptResource->RemoveUsage();
				}
			}

			Device = device::CreateDevice();
			Render = video::CreateRender(video::GDT_OPENGL);
			JENEW(RenderWindow,video::jeRenderWindow(Device,Render,jeVector2di(core::GetVariables()->GetVariables32("Window_Width"),core::GetVariables()->GetVariables32("Window_Height")),"Enesea Game",core::GetVariables()->GetVariablec8("Window_VSync") ? true : false,core::GetVariables()->GetVariablec8("Window_Fullscreen") ? true : false,core::GetVariables()->GetVariablec8("Window_Depth"),core::GetVariables()->GetVariablec8("Window_Hertz"),core::GetVariables()->GetVariablec8("Window_MSAA")))

			Perspective_Game = CreateGamePerspective(RenderWindow);
			core::RegisterPerspective(Perspective_Game);
			Perspective_Game->SetRun(true);

			Perspective_Console = CreateConsolePerspective(RenderWindow);
			core::RegisterPerspective(Perspective_Console);
			Perspective_Console->SetRun(true);
		}

		//! Destructor
		CGameApplication::~CGameApplication()
		{
			core::UnRegisterPerspective(Perspective_Console);
			Perspective_Console->Drop();

			core::UnRegisterPerspective(Perspective_Game);
			Perspective_Game->Drop();

			RenderWindow->Drop();
			Render->Drop();
			Device->Drop();
		}

		//! Tick
		void CGameApplication::Tick()
		{
			if(Perspective_Game && (!Perspective_Game->IsRun()))
				SetRun(false);
			if(Perspective_Console && (!Perspective_Console->IsRun()))
				SetRun(false);

			//if(core::GetFpsCounter()->IsUpdated())
			//	RenderWindow->GetWindow()->SetTitle(jeStringd("Fps : ")+jeStringd((s32)core::GetFpsCounter()->GetFps()));

			RenderWindow->GetWindow()->GetKeyDriver()->FlushKeysHits();
			RenderWindow->GetWindow()->GetKeyDriver()->FlushKeysStream();

			Device->DevicePoll();

			//core::GetSystem()->SystemSleep(1);
		}

		//! Begin Update
		void CGameApplication::BeginUpdate()
		{
		}

		//! End Update
		void CGameApplication::EndUpdate()
		{
		}

		//! Begin Render
		void CGameApplication::BeginRender()
		{
			Render->Begin();
		}

		//! End Render
		void CGameApplication::EndRender()
		{
			Render->End();
		}

		//! Set Run
		void CGameApplication::SetRun(u1 Mode)
		{
			Run = Mode;
		}

		//! Return true if run
		u1 CGameApplication::IsRun()
		{
			return Run;
		}

		//! Create Game Application
		IGameApplication * JE_GAME_FUNCTION_CALL CreateGameApplication()
		{
			IGameApplication * Application = NULL;
			JENEW(Application,CGameApplication)
			return Application;
		}

	}
}

